home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Scribefire-1.4.2 / scribefire-1.4.2-fx+fl.xpi / chrome / content / spellcheck.js < prev    next >
Encoding:
JavaScript  |  2007-02-24  |  7.6 KB  |  211 lines

  1. /*
  2.     Performancing for Firefox Spell Check for Firefox 2.0beta1
  3.     
  4.     A modified version of the JS from textbox.xml since editor's do not inherite spellchecking (why?)
  5.     This spell checking code only works in Firefox 2.0RC1
  6. */
  7. var gSpelltest = null;
  8.  
  9. function pffSpellCheck() {
  10.     this.isFx2orGreater = false;
  11.     this.prefs = null;
  12. }
  13.  
  14. pffSpellCheck.prototype.init = function() {
  15.     var isFX2 = this.isFX2OrGreater();
  16.     if(isFX2){
  17.         var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
  18.         loader.loadSubScript("chrome://global/content/inlineSpellCheckUI.js", this);
  19.     }
  20. }
  21.  
  22. pffSpellCheck.prototype.isPrefSet = function() {
  23.     var ifPrefTrue = false;
  24.       try{
  25.           return gPerformancingUtil.prefs.getBoolPref("extra.doSpellCheck");
  26.       }catch(e){}
  27.       return true;
  28. }
  29.  
  30. pffSpellCheck.prototype.isFX2OrGreater = function() {
  31.       var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  32.                         .getService(Components.interfaces.nsIXULAppInfo);
  33.       var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
  34.                                    .getService(Components.interfaces.nsIVersionComparator);
  35.       if(versionChecker.compare(appInfo.version, "2.0") >= 0) {
  36.         this.isFx2orGreater = true;
  37.         return true;
  38.       }else{
  39.           this.isFx2orGreater = false;
  40.       }
  41.       return false;
  42. }
  43.  
  44. pffSpellCheck.prototype.setupTextBoxSpellCheck = function() {
  45.       try{
  46.           this.init();
  47.           if(this.isFx2orGreater && this.isPrefSet() ){
  48.               //Enable spellcheck for the source editor
  49.               var theSourceInput = document.getElementById("performancing-message-source");
  50.               var theEditor = theSourceInput.inputField.QueryInterface(Components.interfaces.nsIDOMNSEditableElement).editor;
  51.               this.InlineSpellCheckerUI.init(theEditor);
  52.               
  53.               this.InlineSpellCheckerUI.mEditor = theEditor;
  54.               this.InlineSpellCheckerUI.mInlineSpellChecker = this.InlineSpellCheckerUI.mEditor.inlineSpellChecker;
  55.               this.InlineSpellCheckerUI.enabled = true;
  56.           }else{
  57.               this._setNoSpellCheckingAllowed();
  58.           }
  59.       }catch(e){}
  60. }
  61.  
  62. pffSpellCheck.prototype.setupEditorSpellCheck = function() {
  63.     try{
  64.           this.init();
  65.           if(this.isFx2orGreater && this.isPrefSet() ){
  66.               //Enable spellcheck for the rich editor
  67.               if( gTheTabHTMLEditor.isDocumentEditable ){
  68.                   this.InlineSpellCheckerUI.mEditor = gTheTabHTMLEditor;
  69.                   this.InlineSpellCheckerUI.mInlineSpellChecker = this.InlineSpellCheckerUI.mEditor.inlineSpellChecker;
  70.                   this.InlineSpellCheckerUI.enabled = true;
  71.               }
  72.         }else{
  73.             //Hide the menu stuff
  74.             this._setNoSpellCheckingAllowed();
  75.         }
  76.     }catch(e){
  77.     }
  78. }
  79.  
  80. pffSpellCheck.prototype._delayedInitSpellCheck = function(popupNode){
  81.     
  82. }
  83.     
  84. pffSpellCheck.prototype._doPopupItemEnabling = function(popupNode){
  85.     if(this.isFx2orGreater){
  86.     // -- spell checking --
  87.  
  88.             // try to find the outer textbox for this box, don't search
  89.             // up too far.
  90.             gSpelltest = popupNode;
  91.             /*
  92.             var textboxElt = popupNode;
  93.             for (var i = 0; i < 5; i ++) {
  94.               if (textboxElt.localName == "editor")
  95.                 break;
  96.               textboxElt = textboxElt.parentNode;
  97.             }
  98.             if (textboxElt.tagName != "editor") {
  99.               // can't find it, give up
  100.               this._setNoSpellCheckingAllowed();
  101.               return;
  102.             }
  103.             */
  104.             //var spellui = textboxElt.spellCheckerUI;
  105.             var spellui = this.InlineSpellCheckerUI;
  106.             this.spellui = spellui;
  107.  
  108.             if (! spellui.canSpellCheck)
  109.             {
  110.               this._setNoSpellCheckingAllowed();
  111.               return;
  112.             }
  113.             
  114.             spellui.initFromEvent(document.popupRangeParent, document.popupRangeOffset);
  115.  
  116.             var enabled = spellui.enabled;
  117.             document.getElementById("spell-check-enabled").setAttribute("checked", enabled);
  118.  
  119.             var overMisspelling = spellui.overMisspelling;
  120.             this._setMenuItemVisibility("spell-add-to-dictionary", overMisspelling);
  121.             this._setMenuItemVisibility("spell-suggestions-separator", overMisspelling);
  122.  
  123.             // suggestion list
  124.             var spellSeparator = document.getElementById("spell-suggestions-separator");
  125.             var numsug = spellui.addSuggestionsToMenu(popupNode, spellSeparator, 5);
  126.             this._setMenuItemVisibility("spell-no-suggestions", overMisspelling && numsug == 0);
  127.  
  128.             // dictionary list
  129.             var dictmenu = document.getElementById("spell-dictionaries-menu");
  130.             //var dictmenu = document.getElementById("spell-dictionaries");
  131.             var numdicts = spellui.addDictionaryListToMenu(dictmenu, null);
  132.             this._setMenuItemVisibility("spell-dictionaries", enabled && numdicts > 1);
  133.     }
  134. }
  135.  
  136. pffSpellCheck.prototype._doPopupItemDisabling = function(popupNode){
  137.     if (this.isFx2orGreater && this.spellui) {
  138.             this.spellui.clearSuggestionsFromMenu();
  139.             this.spellui.clearDictionaryListFromMenu();
  140.     }
  141. }
  142.  
  143. pffSpellCheck.prototype._setMenuItemVisibility = function(anonid, visible){
  144.     try{
  145.         document.getElementById(anonid).hidden = !visible;
  146.     }catch(e){}
  147. }
  148.  
  149. pffSpellCheck.prototype._setNoSpellCheckingAllowed = function(){
  150.     this._setMenuItemVisibility("spell-no-suggestions", false);
  151.     this._setMenuItemVisibility("spell-check-enabled", false);
  152.     this._setMenuItemVisibility("spell-check-separator", false);
  153.     this._setMenuItemVisibility("spell-add-to-dictionary", false);
  154.     this._setMenuItemVisibility("spell-suggestions-separator", false);
  155.     this._setMenuItemVisibility("spell-dictionaries", false);
  156. }
  157.  
  158. pffSpellCheck.prototype.addToDictionary = function(){
  159.     try{
  160.         this.spellui.addToDictionary();
  161.     }catch(e){}
  162. }
  163.  
  164. pffSpellCheck.prototype.toggleEnabled = function(){
  165.     try{
  166.         this.spellui.toggleEnabled();
  167.     }catch(e){}
  168. }
  169.  
  170. pffSpellCheck.prototype.spellCheckNow = function(){
  171.     this.spellui.mInlineSpellChecker.enableRealTimeSpell = true;
  172. }
  173. pffSpellCheck.prototype.addDictionaries = function(){
  174.     if(!this.prefs){
  175.         this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  176.     }
  177.     
  178.     var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"].getService(Components.interfaces.nsIURLFormatter);
  179.     var uri = formatter.formatURLPref("browser.dictionaries.download.url");
  180.     
  181.     var locale = "-";
  182.     try {
  183.       locale = this.prefs.getComplexValue("intl.accept_languages",
  184.                              Components.interfaces.nsIPrefLocalizedString).data;
  185.     }
  186.     catch (e) { }
  187.     
  188.     var version = "-";
  189.     try {
  190.       version = Components.classes["@mozilla.org/xre/app-info;1"]
  191.                          .getService(Components.interfaces.nsIXULAppInfo)
  192.                          .version;
  193.     }
  194.     catch (e) { }
  195.     
  196.     uri = uri.replace(/%LOCALE%/, escape(locale));
  197.     uri = uri.replace(/%VERSION%/, version);
  198.     
  199.     var newWindowPref = this.prefs.getIntPref("browser.link.open_newwindow");
  200.     var where = newWindowPref == 3 ? "tab" : "window";
  201.     
  202.     openUILinkIn(uri, where);
  203. }
  204.  
  205. pffSpellCheck.prototype.dummy = function(){
  206.     //foo
  207. }
  208.  
  209.  
  210.  
  211.